package TangramCore;

import java.awt.geom.Rectangle2D;

public class Vect {

   public static boolean kratsiVect(Point2D puv,Point2D novy){
     if((puv.x*puv.x+puv.y*puv.y)>(novy.x*novy.x+novy.y*novy.y)){
       puv.copyFrom(novy);
       return true;
     }
     return false;
   }
   public static Point2D vect(Point2D p1,Point2D p2){
     return new Point2D(p2.x-p1.x,p2.y-p1.y);
   }
   public static Point2D vectCast(Point2D p1,Point2D p2,double n){
     Point2D p = vect(p1,p2);
     p.setLocation(n*p.x+p1.x,n*p.y+p1.y);
     return p;
   }

   public static double getAngle(Point2D p1, Point2D p2, Point2D p3,
       Point2D p4) {
     return getAngle(vect(p1, p2), Vect.vect(p3, p4));
   }

   public static Point2D intersection(Point2D p1, Point2D p2,Point2D p4,Point2D p3,boolean jenUsecky) {
     if (p2.equals(p3))
       return new Point2D(p3);
     double ax = p2.x - p1.x;
     double ay = p2.y - p1.y;
     double bx = p3.x - p4.x;
     double by = p3.y - p4.y;
     double div = (ax * by - ay * bx);
     if(div == 0)
         return null;
     double k = (ax * (p1.y - p4.y) - ay * (p1.x - p4.x)) / div;
     double l = (bx * (p4.y - p1.y) - by * (p4.x - p1.x)) / div;
     if(jenUsecky == false || (l >= 0 && l<=1 && k >= 0 && k<=1))
         return new Point2D(p4.x + k*bx,p4.y + k*by);
     return null;
   }
   
   public static Point2D intersection(Point2D p1, Point2D p2,
       Point2D p3,Point2D p4) {
     if (p2.equals(p3))
       return new Point2D(p3);
     double ax = p2.x - p1.x;
     double ay = p2.y - p1.y;
     double bx = p3.x - p4.x;
     double by = p3.y - p4.y;
     double k = (ax * (p1.y - p4.y) - ay * (p1.x - p4.x)) / (ax * by - ay * bx);
     return new Point2D(p4.x + k*bx,p4.y + k*by);
   }

   public static double getAngle(Point2D p1,Point2D p2) {
     double d = p1.distance(0, 0) * p2.distance(0, 0);
     if (d == 0)
       return Double.NaN;
     double a = Math.abs(Math.acos( (p1.x * p2.x + p1.y * p2.y) / d)); //uf, to mi dalo, obcas z vypoctu
     a = Double.isNaN(a) ? 0 : Math.toDegrees(a);//a * 180 / Math.PI; //vypadne nepatrne vic nez 1 a acos je z toho hotovej...
     return a > 90 ? 180 - a: a;
   }

   public static boolean liesInside(Rectangle2D r1, Rectangle2D r2, double tol){
     return r1.getX() - tol <= r2.getX() && r1.getY() - tol <= r2.getY() &&
            r2.getX() + r2.getWidth() <= r1.getX() + r1.getWidth() + tol &&
            r2.getY() + r2.getHeight() <= r1.getY() + r1.getHeight() + tol;
   }

   public static Point2D midpoint(Point2D dest, Point2D p1,Point2D p2){
     dest.x = (p1.x + p2.x)/2;
     dest.y = (p1.y + p2.y)/2;
     return dest;
   }
   public static Point2D midpoint(Point2D p1,Point2D p2){
     Point2D ret = new Point2D();
     return midpoint(ret, p1, p2);
   }

   public static Point2D normal(Point2D dest, Point2D p1,Point2D p2){
     dest.x = p2.y - p1.y;
     dest.y = p1.x - p2.x;
     return dest;
   }
   public static Point2D normal(Point2D p1,Point2D p2){
     Point2D ret = new Point2D();
     return normal(ret, p1, p2);
   }

   public static void normalize(Point2D p,double length){
     double pom = length / p.distance(0, 0);
     p.x *= pom;
     p.y *= pom;
   }

   public static void negative(Point2D p){
     p.x = -p.x;
     p.y = -p.y;
   }
   public static Point2D getNegative(Point2D p){
     return new Point2D(-p.x,-p.y);
   }
   public static double normalizeAngle(double a){
     double circle = 2 * Math.PI;
     if (a < 0)
       a += circle;
     else if (a > circle)
       a -= circle;
     return a;
   }
   public static Point2D add(Point2D a, Point2D b){
        Point2D ret = new Point2D(a);
        ret.translate(b);
        return ret;
   }
   public static Point2D sub(Point2D a, Point2D b){
        Point2D ret = new Point2D(a);
        ret.translate(-b.x, -b.y);
        return ret;
   }

}
